
from imageReader import *
import cv2
import cv2.cv as cv
import math
import numpy as np
from visualization import drawscaled

def crop(image, factorLeft, factorRight, factorTop, factorDown):
    cutLeft = math.floor(image.shape[1]*factorLeft)
    cutRight = math.floor(image.shape[1]*(1-factorRight))
    cutTop = math.floor(image.shape[0]*factorTop)
    cutDown = math.floor(image.shape[0]*(1-factorDown))
    return (image[cutTop:cutDown,cutLeft:cutRight],cutTop,cutLeft)
        
def contrastEnhance(image):
    return cv2.equalizeHist(image)

def adaptiveThreshold(image):
    gauss = cv2.GaussianBlur(image,(9,9),0)  
    return cv2.adaptiveThreshold(gauss,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV,19,2)

def opening(image):
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
    return cv2.morphologyEx(image,cv2.MORPH_OPEN,kernel)

def closing(image):
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
    return cv2.morphologyEx(image,cv2.MORPH_OPEN,kernel)

def getTeethMap(threshold, image):
    teethMap = np.zeros((image.shape[0],image.shape[1]))
    for rowIndex,row in enumerate(image):
        for columnIndex,value in enumerate(row):
            if(value >= threshold):
                teethMap[rowIndex,columnIndex] = 255
    return teethMap
        

